package com.kecheng.dao;


import com.kecheng.hibernate.BaseDao;
import com.kecheng.pojo.Product;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class ProductDao extends BaseDao{


	public List<Product> findall(int page) {
		// TODO Auto-generated method stub
	
		int rowsPerPage =20;

		DetachedCriteria criteria=DetachedCriteria.forClass(Product.class);
		
		criteria.addOrder(Order.desc("id"));

		List<Product> products= (List<Product>) this.getHibernateTemplate().findByCriteria(criteria, (page-1)*rowsPerPage, rowsPerPage);
		
		return products;
	}

	public Product findbyid(int id) {
		// TODO Auto-generated method stub
		List<Product> products= (List<Product>) this.getHibernateTemplate().find("from Product where id=?",id);

		if(products.size()>0){    
            Product product=products.get(0);       
		return product;      
		}
		return null;
	}

	public List<Product> findbyclassify(int classify,int page) {
		// TODO Auto-generated method stub
		int count =20;
		
	DetachedCriteria criteria=DetachedCriteria.forClass(Product.class);
		criteria.addOrder(Order.desc("id"));
		criteria.add(Restrictions.eq("classify", classify));
		List<Product> products= (List<Product>) this.getHibernateTemplate().findByCriteria(criteria, (page-1)*count, count);
		
	return products;
	}

	public void deletebyid(Product product) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(product);
	}
   
	public int findallpage() {
		// TODO Auto-generated method stub
		String hql = "select count(*) from Product as product";

		Long count = (Long)getHibernateTemplate().find(hql).listIterator().next();
        System.out.println(count);
        int count1=count.intValue();
        int pageSize=20;
        if(count1 % pageSize == 0) {
			return count1/pageSize;
		} else {
			return count1/pageSize+1;
		}
	}

	public List<Product> findrecommonds(int classifyid, String isrecommend) {
		// TODO Auto-generated method stub
		List<Product> products= (List<Product>) this.getHibernateTemplate().find("from Product where oneid=? and isrecommend=?",new Object[]{classifyid,isrecommend});

		return products;
	}

	public List<Product> findbyclassify(int classify) {
		// TODO Auto-generated method stub
		List<Product> products= (List<Product>) this.getHibernateTemplate().find("from Product where classify=?",classify);

		return products;
	}
	

	public List<Product> findbytwo(int twoid) {
		// TODO Auto-generated method stub
		List<Product> products= (List<Product>) this.getHibernateTemplate().find("from Product where twoid=?",twoid);
		return products;
	}

	public List<Product> findbyone(int oneid) {
		// TODO Auto-generated method stub
		List<Product> products= (List<Product>) this.getHibernateTemplate().find("from Product where oneid=?",oneid);
		return products;
	}



	public List<Product> findbyhuiyuan(String huiyuan) {
		// TODO Auto-generated method stub
		List<Product> products= (List<Product>) this.getHibernateTemplate().find("from Product where huiyuan=?",huiyuan);
		return products;
	}

	public List<Product> findbytiyan(String tiyan) {
		// TODO Auto-generated method stub
		List<Product> products= (List<Product>) this.getHibernateTemplate().find("from Product where tiyan=?",tiyan);
		return products;
	}
	
	
	
		
	
	
	public void add(Product product) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(product);
	}

	public void delete(Product product) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(product);
	}

	public List<Product> findall(Integer page,Integer pagesiez) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
		criteria.addOrder(Order.desc("id"));
		List<Product> products = (List<Product>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pagesiez, pagesiez);
		return products;
	}

	public void update(Product product) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(product);
	}
	public Product findbyid(Integer id) {
		// TODO Auto-generated method stub
		List<Product> products = (List<Product>) this.getHibernateTemplate().find(
				"from Product where id=?", id);
		if (products.size() > 0) {
			Product product = products.get(0);
			return product;
		}
		return null;
	}

	public List<Product> findbyclassify(String classify,Integer page,Integer pagesize) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
		criteria.addOrder(Order.desc("id"));
		criteria.add(Restrictions.eq("classify", classify));
		List<Product> products = (List<Product>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pagesize, pagesize);

		return products;
	}


	public List<Product> findindex() {

		int conunt = 20;

		String hql = "from Product  order by id desc";

		HibernateTemplate ht = this.getHibernateTemplate();

		ht.setMaxResults(conunt);

		List<Product> products = (List<Product>) ht.find(hql);

		return products;

	}

	public Product checkbianma(String bianma) {
		List<Product> products = (List<Product>) this.getHibernateTemplate().find(
				"from Product where bianma=?", bianma);

		if (products.size() > 0) {
			Product product = products.get(0);
			return product;
		}
		return null;
	}

	public int findallpage(Integer pagesize) {
		// TODO Auto-generated method stub
		String hql = "select count(*) from Product as product";

		Long count = (Long) getHibernateTemplate().find(hql).listIterator()
				.next();
		System.out.println(count);
		int count1 = count.intValue();
		if (count1 % pagesize == 0) {
			return count1 / pagesize;
		} else {
			return count1 / pagesize + 1;
		}
	}

	public int findsearchpage(String key) {

		String hql = "select count(*) from Product as product where title like '%"
				+ key + "%'or bianma like '%" + key + "%'";
		Long count = (Long) getHibernateTemplate().find(hql).listIterator()
				.next();
		int count1 = count.intValue();
		int pageSize = 20;
		if (count1 % pageSize == 0) {
			return count1 / pageSize;
		} else {
			return count1 / pageSize + 1;
		}

	}

	public List<Product> search(String key, int page) {
		// TODO Auto-generated method stub

		int rowsPerPage = 20;
		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
		criteria.add(Restrictions.or(Restrictions.like("title", key,
				MatchMode.ANYWHERE), Restrictions.like("bianma", key,
				MatchMode.ANYWHERE)));
		criteria.addOrder(Order.desc("id"));
		List<Product> products = (List<Product>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * rowsPerPage, rowsPerPage);
		return products;

	}

	public int findnum() {
		// TODO Auto-generated method stub
		String hql = "select count(*) from Product as product";

		Long count = (Long) getHibernateTemplate().find(hql).listIterator()
				.next();
		int a = new Long(count).intValue();
		return a;
	}

	public List<Product> findall() {
		
		return (List<Product>) getHibernateTemplate().find("from Product");
	}

	public List<Product> findbyclassify(String classify) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
		criteria.addOrder(Order.desc("id"));
		criteria.add(Restrictions.eq("classify", classify));
		List<Product> products = (List<Product>) this.getHibernateTemplate().findByCriteria(
				criteria);
		return products;
	}



	public List<Product> findbyclassify(String classify, Integer page) {
		// TODO Auto-generated method stub
		int rowsPerPage=20;
		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
		criteria.addOrder(Order.desc("id"));
		criteria.add(Restrictions.eq("classify", classify));
		List<Product> products = (List<Product>) this.getHibernateTemplate().findByCriteria(criteria,(page - 1) * rowsPerPage, rowsPerPage);
		return products;

	}

	public List<Product> serchLike(String name) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria=DetachedCriteria.forClass(Product.class);
		criteria.add(
				Restrictions.or(Restrictions.like("title",name, MatchMode.ANYWHERE),
			    Restrictions.like("classify",name,MatchMode.ANYWHERE)));		
		List<Product> products= (List<Product>) this.getHibernateTemplate().findByCriteria(criteria);
		return products;
	}

	public List<Product> findbyTwoClassify(int twoClassifyId) {
		// TODO Auto-generated method stub
		List<Product>products = (List<Product>) this.getHibernateTemplate().find("from Product where twoClassifyId=?",twoClassifyId);
		return products;
	}



	public List<Product> findNew() {
		// TODO Auto-generated method stub
		List<Product> products = (List<Product>) this.getHibernateTemplate().find("from Product order by id desc");
		return products;
	}
	
	
 }
